--
-- Author: yawei.zhu
-- Date: 2018-12-15 15:04:04
--
-- PvpChallengeMatchingView
--

local PvpChallengePlayerRender = import(".renders.PvpChallengePlayerRender")
local CrossServerEliteMatchHelper = require("app.pvp.views.eliteMatch.CrossServerEliteMatchHelper")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.PvpChallengeMatchingScene

function ClassRef:init()

	self._titlePanel = self._ui["Panel/Panel_content/Panel_top"]
	self._titleTxt = self._ui["Panel/Panel_content/Panel_top/text"]
	self._titleImg = self._ui["Panel/Panel_content/Panel_top/img"]

	self._ui.Button_readyCancel:addClickEventListener(function(sender)
		local createTeamDialog = display.newConfirmDialog(L("challengeTips10"), function() -- 确定
			if self._playId == Enums.TeamTarget.eliteMatchArena 
			or self._playId == Enums.TeamTarget.eliteMatchDuel then
				UD.csEliteMatch:C2S_CrossCombatPrepare(false)
			else
				executeFunc(self._onOperate, "onMatchingCancel")
			end
		end)
		UIHandler:addPopDialog( createTeamDialog )
	end)

	self._ui.Button_readySure:addClickEventListener(function(sender)
		if self._playId == Enums.TeamTarget.eliteMatchArena 
		or self._playId == Enums.TeamTarget.eliteMatchDuel then
			UD.csEliteMatch:C2S_CrossCombatPrepare(true)
		else
			executeFunc(self._onOperate, "onMatchingReady")
		end
	end)

	self._ui.Button_close:addClickEventListener(function(sender)
		self:closeView()
	end)

	local teamPanel = self._ui["Panel/Panel_content/Panel_team"]

	self._playerARenders = {}
	for i = 6, 10 do
		local node = teamPanel:getChildByName("p_"..i)
		local render = PvpChallengePlayerRender.create(node)
		self._playerARenders[i - 5] = render
	end

	self._playerBRenders = {}
	for i = 1, 5 do 
		local node = teamPanel:getChildByName("p_"..i)
		local render = PvpChallengePlayerRender.create(node)
		self._playerBRenders[i] = render
	end

	self._eachWidth = self._playerARenders[1]:getContentSize().width
	self._width = self._eachWidth * 5
end

function ClassRef:_tick()
	local remainedTime = math.ceil(self._endTime - app.session:getServerTime())
	if remainedTime < 1 then
		remainedTime = 1
	end 
	self._ui["Panel/Panel_content/Panel_bom/num"]:setString(remainedTime)
end

function ClassRef:updateView(msg, endTime)
	self._playId = msg.playId
	local numA = #msg.membersA
	local numB = #msg.membersB
	local totalCount = numA + numB
	local myIsReady = false

	print("PvpChallengeMatchingView", numA, numB)

	self._ui["Panel/Panel_content/Panel_bom/ready"]:setString(string.format(L("lua_code_text_90"), msg.readyNum, totalCount))
	self._titleTxt:setString(L("lua_code_text_193"))
	if self._playId == Enums.TeamTarget.eliteMatchArena or
		self._playId == Enums.TeamTarget.eliteMatchDuel then
		local name = CrossServerEliteMatchHelper:getNameByPlayId(self._playId)
		self._titleTxt:setString(string.format(L("EliteMatchTip49"), name))
	end
	display.setNodesMiddle(self._titlePanel, self._titleImg, self._titleTxt)

	local myTeamGroup = 2
	for i = 1, numA do
		if msg.membersA[i].info.uid == UD:getUid() then
			myTeamGroup = 1
		end
	end

	for i = 1, 5 do
		self._playerARenders[i]:setVisible(false)
		self._playerBRenders[i]:setVisible(false)

		if i <= numA then
			self._playerARenders[i]:setVisible(true)
			local value = {}
			value.IsReady = msg.membersA[i].IsReady
			value.baseInfo = UserBaseInfo.new(msg.membersA[i].info)
			if value.baseInfo.uid == UD:getUid() then
				myIsReady = value.IsReady
			end
			if myTeamGroup == 2 then
				value.baseInfo.name = "？？？"
			end

			-- print(value.baseInfo.uid, value.IsReady, UD:getUid())
			self._playerARenders[i]:setData(value)
		end
		if i <= numB then
			self._playerBRenders[i]:setVisible(true)
			local value = {}
			value.IsReady = msg.membersB[i].IsReady
			value.baseInfo = UserBaseInfo.new(msg.membersB[i].info)
			if value.baseInfo.uid == UD:getUid() then
				myIsReady = value.IsReady
			end
			if myTeamGroup == 1 then
				value.baseInfo.name = "？？？"
			end
			-- print(value.baseInfo.uid, value.IsReady, UD:getUid())
			self._playerBRenders[i]:setData(value)
		end
	end

	-- 排列Render
	local midA = (1 + numA) / 2
	for i = 1, numA do
		local posX = self._width / 2 - (midA - i) * self._eachWidth
		self._playerARenders[i]:setPositionX(posX - self._eachWidth / 2)
	end

	local midB = (1 + numB) / 2
	for i = 1, numB do
		local posX = self._width / 2 - (midB - i) * self._eachWidth
		self._playerBRenders[i]:setPositionX(posX - self._eachWidth / 2)
	end

	-- 自己的状态
	if myIsReady then
		display.setBtnTouchEnable(self._ui.Button_readyCancel, false)
		display.setBtnTouchEnable(self._ui.Button_readySure, false)
	else
		display.setBtnTouchEnable(self._ui.Button_readyCancel, true)
		display.setBtnTouchEnable(self._ui.Button_readySure, true)
	end

	-- 定时器
	if self._cronsIdx then
        scheduler.cancelCron(self._cronsIdx)
        self._cronsIdx = nil
    end
	self._endTime = endTime
	self._cronsIdx = scheduler.newCron(handler(self, self._tick), 1, true)
	self:_tick() -- 先执行一次tick
end

-- 关掉界面时，移除定时器
function ClassRef:onExit()
    if self._cronsIdx then
        scheduler.cancelCron(self._cronsIdx)
        self._cronsIdx = nil
    end

    self.super.onExit(self)
end

return ClassRef
